import sys, os, glob

cflags   = '-ggdb -Wall -std=gnu99'
#defs	 = []
defs	 = ['WITH_CONN']
includes = ['.']
libpath  = ['.']
libs     = ['m', 'pthread']
name     = 'libshared.a'
libfiles = glob.glob("*.c")
check_malloc = False

if not 'WITH_CONN' in defs:
	libfiles.remove('conn.c')

libtcmalloc = '/usr/local/lib/libtcmalloc_minimal.a'
usetcmalloc = False
try:
	Import('debugdefs')
	defs += debugdefs
	if 'TCMALLOC' in debugdefs:
		usetcmalloc = True
		print '====== base use tcmalloc from parent! ======'
	else:
		print '====== base use gun malloc from parent! ======'
except:
	if check_malloc:
		if os.path.isfile(libtcmalloc):
			print '====== base use google tcmalloc! ======'
			usetcmalloc = True
			defs.append('TCMALLOC')
		else:
			print '====== base use gnu malloc! ======'
	else:
		print '====== base use gnu malloc! ======'

if usetcmalloc:
	#libfiles.append(libtcmalloc)
	
	if not os.path.isdir('tcmalloc'):
		os.system('mkdir tcmalloc')
	os.system("cp %s tcmalloc" % libtcmalloc)
	old = os.getcwd()
	os.chdir('tcmalloc')
	os.system("ar x %s" % os.path.basename(libtcmalloc))
	os.chdir(old)
	libfiles += glob.glob("tcmalloc/*.o")

	libs.append('stdc++')

if 'debug' in  BUILD_TARGETS:
    cflags += ' -DDEBUG -DDEBUGMEM'
    BUILD_TARGETS[0] = 'libbase.a'

    env = Environment(CCFLAGS=cflags, CPPPATH=includes, LIBPATH=libpath, LIBS=libs, CPPDEFINES=defs)
    bin = env.StaticLibrary(name, libfiles)
else:
    cflags += ' -O2'
    env = Environment(CCFLAGS=cflags, CPPPATH=includes, LIBPATH=libpath, LIBS=libs, CPPDEFINES=defs)
    bin = env.StaticLibrary(name, libfiles)

install_dir = '../../target/libs'
env.Install(install_dir, bin)
env.Alias('install', install_dir)


